<template>
    <el-table
        :data="tableData"
        style="margin-bottom:20px;width:100%;"
        class="ccui-form-child-table"
        border
        :summary-method="getSummaries"
        show-summary
    >
        <el-table-column
            :show-overflow-tooltip="true"
            label="序号"
            type="index"
            align="center"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="batchNumber"
            label="批次号"
            min-width="220"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="materialCode"
            label="设备物资编码"
            min-width="200"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="materialName"
            label="设备物资名称"
            min-width="300"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="materialState"
            label="设备物资说明"
            min-width="130"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="categoryName"
            label="分类名称"
            min-width="220"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="measureUnit"
            label="计量单位"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="storageCount"
            label="库存数量"
            min-width="120"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="exwarehouseCount"
            label="冲销数量"
            min-width="120"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="currencyName"
            label="币种"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="exchangeRate"
            label="汇率"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="unitPriceWithTax"
            label="含税单价"
            align="right"
            header-align="left"
            min-width="140"
        />
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="exwarehouseMoney"
            label="冲销金额"
            align="right"
            header-align="left"
            min-width="150"
        />
        
        <!--        <el-table-column-->
        <!--            :show-overflow-tooltip="true"-->
        <!--            prop="company"-->
        <!--            label="含税单价RMB"-->
        <!--        />-->
        <!--        -->
        <!--        <el-table-column-->
        <!--            :show-overflow-tooltip="true"-->
        <!--            prop="moneyWithTax"-->
        <!--            label="含税金额RMB"-->
        <!--        />-->
        
        <el-table-column
            :show-overflow-tooltip="true"
            prop="remark"
            label="备注"
        />
    </el-table>
</template>

<script>

export default {
    name: "tableList",
    data() {
        return {
            tableData: []
        }
    },
    created() {
        this.$bus.$on("getTableData", data => {
            this.tableData = data;
            this.tableData.forEach(item => {
                item.storageCount = (+item.storageCount).toFixed(3);
                item.exwarehouseCount = (0 - item.exwarehouseCount).toFixed(3);
                item.unitPriceWithTax = (+item.unitPriceWithTax).toFixed(2);
                item.exwarehouseMoney = (0 - item.exwarehouseMoney).toFixed(2);
            });
        });
        
        this.$bus.$on("clearTable", () => {
            this.tableData = [];
        })
    },
    methods: {
        getSummaries(param) {
            const {columns, data} = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = "合计"
                }
                const values = data.map(item => +item[column.property]);

                if (!values.every(value => isNaN(value))) {
                    sums[index] = values.reduce((pre, curr) => {
                        const value = +curr
                        if (!isNaN(value)) {
                            if (column.property === "exwarehouseCount") {
                                return new this.$BigNumber(+pre).plus(+curr).toFixed(3);
                            } else if (column.property === "exwarehouseMoney") {
                                return new this.$BigNumber(+pre).plus(+curr).toFixed(2);
                            }
                        } else {
                            return pre;
                        }
                    }, 0);
                }
            });
            return sums;
        }
    },
    destroyed() {
        this.$bus.$off("getTableData");
        this.$bus.$off("clearTable");
    }
}
</script>

<style scoped lang="scss">

</style>